//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package user_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type UserSecretDbItem struct {
	UserId string `bson:"_id"`
	Secret string `bson:"secret"`
}

type _UserSecretDao struct {
	mc *mongo.Collection
}

func newUserSecretDao(db *mongo.Database) (*_UserSecretDao, error) {
	mc := db.Collection(db_ns.DB_NS_USER + "secret")
	return &_UserSecretDao{mc: mc}, nil
}

func (db *_UserSecretDao) Insert(ctx context.Context, dbItem *UserSecretDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_UserSecretDao) Exist(ctx context.Context, userId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": userId})
	return count > 0, err
}

func (db *_UserSecretDao) Get(ctx context.Context, userId string) (*UserSecretDbItem, error) {
	retItem := &UserSecretDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": userId}).Decode(retItem)
	return retItem, err
}
